package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.MemberJAType;
import com.service.dao.mapper.JournalAccountMapper;
import com.service.dao.mapper.MemberStoreMapper;
import com.service.dao.model.JournalAccount;
import com.service.dao.model.MemberStore;
import com.service.scheduling.dto.MemberStoreChange;
import com.service.scheduling.service.MemberStoreService;
import com.service.scheduling.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月24日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
@SuppressWarnings("all")
public class MemberStoreServiceImpl extends ServiceImpl<MemberStoreMapper, MemberStore> implements MemberStoreService {

    @Autowired
    private JournalAccountMapper journalAccountMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberStore getMemberStore(Integer memberId, String coinsName) {
        return getMemberStore(memberId, CoinsEnum.valueOf(coinsName));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberStore getMemberStore(Integer memberId, CoinsEnum coin) {
        MemberStore store = getOne(new QueryWrapper<MemberStore>().lambda()
                .eq(MemberStore::getMemberId, memberId)
                .eq(MemberStore::getCoinsName, coin.getName()));
        if (ObjectUtils.isEmpty(store)) {
            store = new MemberStore();
            store.setMemberId(memberId);
            store.setCoinsId(coin.getCode());
            store.setCoinsName(coin.getName());
            store.setNum(BigDecimal.ZERO);
            store.setFrostNum(BigDecimal.ZERO);
            store.setCreateTime(new Date());
            save(store);
        }

        return store;
    }

    @Override
    @Transactional
    public Integer changeMemberStore(MemberStoreChange storeChange) {
        MemberStore memberStore = getMemberStore(storeChange.getMemberId(), storeChange.getCoinsName());
        return changeMemberStore(memberStore, storeChange);
    }

    @Transactional
    public Integer changeMemberStore(MemberStore memberStore, MemberStoreChange storeChange) {
        Integer storeId = memberStore.getId();

//        storeChange.getJaType().doOptStore(memberStore, storeChange.getChangeNum());
        BigDecimal storeNumNow = memberStore.getNum().add(storeChange.getChangeNum());
        BigDecimal frostNum = memberStore.getFrostNum();
        JournalAccount journalAccount = JournalAccount.builder()
                .storeId(memberStore.getId())
                .storeNum(storeNumNow)
                .coinsId(memberStore.getCoinsId())
                .coinsName(memberStore.getCoinsName())
                .storeFrostNum(memberStore.getFrostNum())
                .memberId(storeChange.getMemberId())
                .jaNum(storeChange.getChangeNum())
                .custodyFee(storeChange.getCustodyFee())
                .jaType(storeChange.getJaType().getCode())
                .jaRemark(storeChange.getRemark())
                .orderId(storeChange.getOrderId())
                .orderDetailId(storeChange.getOrderDetailId())
                .jaId(storeChange.getJaId())
                .createTime(new Date())
                .build();

        memberStore = new MemberStore();
        memberStore.setId(storeId);
        memberStore.setNum(storeNumNow);
        memberStore.setFrostNum(frostNum);
        updateById(memberStore);

        journalAccountMapper.insert(journalAccount);

        return journalAccount.getId();
    }

    @Override
    public List<JournalAccount> getMiningRecord(Integer memberId, Integer orderId, Date someDay) {
        Date someDayStartTime = DateUtil.getDayOfStart(someDay);
        Date someDayEndTime = DateUtil.getDayOfEnd(someDayStartTime);
        return journalAccountMapper.selectList(new QueryWrapper<JournalAccount>().lambda()
                .eq(JournalAccount::getMemberId, memberId)
                .eq(JournalAccount::getJaType, MemberJAType.MINING.getCode())
                .eq(JournalAccount::getOrderId, orderId)
//                .eq(JournalAccount::getOrderDetailId, orderDetailId)
                .ge(JournalAccount::getCreateTime, someDayStartTime)
                .lt(JournalAccount::getCreateTime, someDayEndTime));
    }

    @Override
    public List<JournalAccount> getFrostMiningRecord(Integer memberId, Integer orderId, Date someDay) {
        Date someDayStartTime = DateUtil.getDayOfStart(someDay);
        Date someDayEndTime = DateUtil.getDayOfEnd(someDayStartTime);
        return journalAccountMapper.selectList(new QueryWrapper<JournalAccount>().lambda()
                .eq(JournalAccount::getMemberId, memberId)
                .eq(JournalAccount::getJaType, MemberJAType.MINING_FROST.getCode())
                .eq(JournalAccount::getOrderId, orderId)
//                .eq(JournalAccount::getOrderDetailId, orderDetailId)
                .ge(JournalAccount::getCreateTime, someDayStartTime)
                .lt(JournalAccount::getCreateTime, someDayEndTime));
    }
}
